u`
p.181u\bh̍ċAĂяov

p.181 \bh̍ċAĂяo

EC#Ȃǂł̓\bh̓ŎgĂяoBċA(J[W)ƂAp邱ƂŃASYVvɕ\łꍇ
EȂAċÂ݂LqƖ[v܂̓̎gɂُIN̂ŁAċAI点Kv
EpFK(q)AtB{ib`(q)Aő񐔁AVF\[gȂ

p.181 KvZ

E鐮n1܂ł̑S̐ςn̊KƂn!ƕ\B
@F 4! = 4~3~2~1 = 24A5! = 5~4~3~2~1 = 120
EāAn! = n ~ (n - 1)! ƁAċAŕ\ƃVvŌJԂ\svɂȂ
EK̃[ƂāA0! = 1! = 1 ̂ŁAċȀIɗpƗǂ

p.182 fact01.cs

//p.182 fact01.cs
using System;
class Fact { //CX^X\bhbĥ݂܂ރNX
    public long CalcFact(int n) { //ċA܂ރCX^X\bhŊKԂ
        long fact; //p̃[Jϐ
        if (n == 0) { //0!͌ŒȂ̂ōċȀIɗp
            fact = 1; //0! = 1
        } else {
            fact = n * CalcFact(n - 1); //n! = n~(n-1)!
        }
        return fact; //n!Ԃ
    }
}
class fact01 {
    public static void Main() {
        Fact f = new Fact();
        for (int i = 0; i <= 20; i++) {
            Console.WriteLine("{0}! = {1}", i, f.CalcFact(i));
        }
    }
}

AWKFp.182 fact01.cs

E0! = 1݂̂Ȃ炸A1! = 1ł邱ƂĎsグ悤
E܂AZqpċLqVvɂ悤

쐬

//AWKFp.182 fact01.cs
using System;
class Fact { //CX^X\bhbĥ݂܂ރNX
    public long CalcFact(int n) { //ċA܂ރCX^X\bhŊKԂ
        return (n <= 1) ? 1 : n * CalcFact(n - 1); //yύXz
    }
}
class fact01 {
    public static void Main() {
        Fact f = new Fact();
        for (int i = 0; i <= 20; i++) {
            Console.WriteLine("{0,2}! = {1}", i, f.CalcFact(i));
        }
    }
}

p.185 fibonacci.cs

//p.185 fibonacci.cs
using System;
class fibo {
    public long CalcFibo(int n) {
        long fb;
        if (n == 1 || n == 2) { //ċȀI
            fb = 1;
        } else {
            fb = CalcFibo(n - 1) + CalcFibo(n - 2); //ċA
        }
        return fb;
    }
}
class fibonacci {
    public static void Main() {
        fibo f = new fibo();
        for (int i = 1; i <= 30; i++) {
            Console.WriteLine("f({0}) = {1}", i, f.CalcFibo(i));
        }
    }
}

AWKFp.185 fibonacci.cs

EZqpċLqVvɂ悤

쐬

//AWKFp.185 fibonacci.cs
using System;
using System.Runtime.Remoting.Messaging;

class fibo {
    public long CalcFibo(int n) {
        return (n <= 2) ? 1L : (long)CalcFibo(n - 1) + CalcFibo(n - 2); //ċA
    }
}
class fibonacci {
    public static void Main() {
        fibo f = new fibo();
        for (int i = 1; i <= 30; i++) {
            Console.WriteLine("f({0}) = {1}", i, f.CalcFibo(i));
        }
    }
}

p.188ilnƎQƓnj

EC#̃\bh̊{͒lnłA̒lɃRs[̂ŁA\bhŉ̒lύXĂAĂяo̎ɂ͔fȂB
@ p.189 swap01.cs
EC#̎Qƌ^f[^ɂĂlnɂȂ̂ŁAQƌ^łstring^̕ϐłĂQƓnɂ͂ȂȂ
@ p.190 swap02.cs
EȂAzȂǂ̃f[^\ɂꍇA\QƂ̂ŁAQƂRs[A\bhŉ̃f[^\̒̒lύXƁAĂяo̎̃f[^\ɔf
@ returnKv͂Ȃ

p.191 charngearray01.cs

//p.191 charngearray01.cs
using System;
class change {
    public void modify(int[] array) { //zȂ̎QƂ󂯎郁\bh
        int n = array.Length; //QƌoRŎ̃vpeB(q)sėvf
        for (int i = 0; i < n; i++) { //vfpđSvfɂČJԂ
            array[i] *= 2; //vf̒l̂ŎɂȂĂz̗vflς
        }
    }
}
class changearray01 {
    public static void Main() {
        change c = new change(); //
        int[] myarray = new int[3]{1, 2, 3};
        Console.WriteLine("----modify\bhsO----");
        int i = 0;
        foreach (int x in myarray) { //z̑SvfɂČJԂ
            Console.WriteLine("myarray[{0}] = {1}", i, x);
            i++; //YȂ̂ŃJE^̑
        }
        c.modify(myarray);
        Console.WriteLine("----modify\bhs----");
        i = 0;
        foreach (int x in myarray) { //z̑SvfɂČJԂ
            Console.WriteLine("myarray[{0}] = {1}", i, x);
            i++; //YȂ̂ŃJE^̑
       }
    }
}

AWKFp.191 charngearray01.cs

Ez̏VvɂAforeachœŶ͔Ȃ̂forɂ悤

쐬

//AWKFp.191 charngearray01.cs
using System;
class change {
    public void modify(int[] array) { //zȂ̎QƂ󂯎郁\bh
        int n = array.Length; //QƌoRŎ̃vpeB(q)sėvf
        for (int i = 0; i < n; i++) { //vfpđSvfɂČJԂ
            array[i] *= 2; //vf̒l̂ŎɂȂĂz̗vflς
        }
    }
}
class changearray01 {
    public static void Main() {
        change c = new change();
        int[] myarray = {1, 2, 3}; //yύXz
        Console.WriteLine("----modify\bhsO----");
        for (int i = 0; i < myarray.Length; i++) { //yύXzz̑SvfɂČJԂ
            Console.WriteLine("myarray[{0}] = {1}", i, myarray[i]); //yύXz
        }
        c.modify(myarray);
        Console.WriteLine("----modify\bhs----");
        for (int i = 0; i < myarray.Length; i++) { //yύXzz̑SvfɂČJԂ
            Console.WriteLine("myarray[{0}] = {1}", i, myarray[i]); //yύXz
        }
    }
}

p.18irefj

EQƓnɂāA\bhŉ̒lύXƁAĂяo̎ɔf̂refwB
EPƂ̕ϐłAƉ̗refw肪Ă邱ƂKvAϐ͈قȂOK
E֐̏F ANZXCq ߂l^ \bh(ref ^ , c){c}
@ ߂l^voidłǂ(returnsv)BꍇArefw̗L݂ł
EďȍF \bh(ref łϐ, c);
EA͏邢͒l̑Ă邱

p.193 swap03.cs

//p.193 swap03.cs
using System;
class myclass {
    private int temp; //̍Ɨp̕ϐ
    public void swap(ref int x, ref int y) { //refw̉Ȃ̂ŎQƓnɂȂ
        temp = x;
        x = y; //x̒l͌Ăяo̕ϐɔf
        y = temp; //y̒l͌Ăяo̕ϐɔf
    }
}
class swap01 {
    public static void Main() {
        myclass s = new myclass();
        int x = 10, y = 20;
        s.swap(ref x, ref y); //refw̎Ȃ̂ŎQƓnɂȂ
        Console.WriteLine("x = {0}, y = {1}", x, y); //Ăяoɂxy̒lĂ
    }
}

AWKFp.193 swap03.cs

EƉقȂ閼OɂāAQƓňʂ₷悤
EmyclassNX̃CX^XϐtemṕiCX^XϐłKvȂ̂Łj[Jvar^ɂ悤

쐬

//AWKFp.193 swap03.cs
using System;
class myclass {
    public void swap(ref int x, ref int y) { //refw̉Ȃ̂ŎQƓnɂȂ
        var temp = x; //yύXzꎞIϐx̒lޔ
        x = y; //x̒l͌Ăяo̕ϐɔf
        y = temp; //y̒l͌Ăяo̕ϐɔf
    }
}
class swap01 {
    public static void Main() {
        myclass s = new myclass();
        int a = 10, b = 20;
        s.swap(ref a, ref b); //refw̎Ȃ̂ŎQƓn(axAby)ɂȂ
        Console.WriteLine("a = {0}, b = {1}", a, b); //Ăяoɂxy̒lĂ
    }
}

p.194ioutL[[hɂQƓnjv

ErefL[[h̋łŁu͏邢͒l̑Ă邱ƁvsvɂȂ
EoutL[[hw肵\bȟĂяoȍ~ł΁A̒l𗘗pł
@ ApۂƓC#̃o[WɈˑ̂ŁAo[W7Oł͎gȂ̂Œ
@ ܂Aoutwɂ͏邢͒l̑ĂȂƂOň̂ŁẢEӂȂǂɗpƃG[ɂȂ̂ŁȀꍇrefɂ邱

AWK p.193 swap03.cs

ErefoutɕςƃG[ɂȂ邱ƂmF悤
Eutemp = x;vux = y;v́Axy邢͒l̑ĂȂ\̂ŃG[ɂȂ

쐬iG[ɂȂj

//AWKFp.193 swap03.cs
using System;
class myclass {
    public void swap(out int x, out int y) { //yύXzoutw̉Ȃ̂ŎQƓnɂȂ
        var temp = x; //yG[zxĂȂ\̂ős
        x = y; //yG[zyĂȂ\̂ős
        y = temp; //y̒l͌Ăяo̕ϐɔf
    }
}
class swap01 {
    public static void Main() {
        myclass s = new myclass();
        int a = 10, b = 20;
        s.swap(out a, out b); //outw̎Ȃ̂ŎQƓn(axAby)ɂȂ
        Console.WriteLine("a = {0}, b = {1}", a, b); //Ăяoɂxy̒lĂ
    }
}

p.194 outkeyword01.cs

//p.194 outkeyword01.cs
using System;
class MyClass {
    public void Square(double x, double y, out double s) { //s͎QƓn
        s = x * y; //̍ӂȂ̂ŏĂȂĂǂ
    }
}
class outkeyword01 {
    public static void Main() {
        double a = 125.3, b = 16.25, c;
        MyClass mc = new MyClass();
        //cɂ͒lĂ܂
        mc.Square(a, b, out c); //c͌ʂ̎󂯎pȂ̂ŏsv
        Console.WriteLine("c{0}m, {1}m̒`̖ʐς{2}[g", a, b, c);
    }
}

AWKFp.194 outkeyword01.cs

ȄꍇAoutL[[hAsvŁAreturnłł邱ƂmF悤

쐬

//AWKFp.194 outkeyword01.cs
using System;
class MyClass {
    public double Square(double x, double y) { //yύXz
        return x * y; //yύXz
    }
}
class outkeyword01 {
    public static void Main() {
        double a = 125.3, b = 16.25; //yύXz
        MyClass mc = new MyClass();
        Console.WriteLine("c{0}m, {1}m̒`̖ʐς{2}[g", a, b, mc.Square(a, b)); //yύXz
    }
}

AWKFp.194 outkeyword01.cs

EoutL[[h֗ȗƂāAQ̘aƐςԂ\bh 
@public void AddMul(double x, double y , out double sum, out double mul) 
@ɂĂ݂悤

쐬

//AWKFp.194 outkeyword01.cs
using System;
class MyClass {
    public void AddMul(double x, double y , out double sum, out double mul) { //yύXz
        sum = x + y; mul = x * y; //yύXz
    }
}
class outkeyword01 {
    public static void Main() {
        double a = 125.3, b = 16.25, c, d; //yύXz
        MyClass mc = new MyClass();
        mc.AddMul(a, b, out c, out d); //yύXz
        Console.WriteLine("a{0}, ς{1}", c, d); //yύXz
    }
}

p.195 \bh̃I[o[[h

Eq̒ʂARXgN^ƓlɃ\bhI[o[[h\
EȂA\bh̃VOj`ɂ͖߂l^͊܂܂Ȃ̂ŁA߂l^݂̂قȂ郁\bh̃I[o[[h͔F߂ꂸG[ɂȂ

p.195 overload01.cs

//p.195 overload01.cs
using System;
class manymethods {
    public int Method(int x) {
        Console.WriteLine("P̃o[WĂ΂܂");
        return x + 10;
    }
    public double Method(double x) {
        Console.WriteLine("Q̃o[WĂ΂܂");
        return x * 2;
    }
    public string Method(string x) {
        Console.WriteLine("R̃o[WĂ΂܂");
        return x += "ł";
    }
    public int Method(int x, int y) {
        Console.WriteLine("S̃o[WĂ΂܂");
        return x + y;
    }
}
class overload01 {
    public static void Main() {
        manymethods m = new manymethods();
        Console.WriteLine("̖߂ĺu{0}vł", m.Method(3));      //1̃o[WĂ΂
        Console.WriteLine("̖߂ĺu{0}vł", m.Method(3.2));    //2̃o[WĂ΂
        Console.WriteLine("̖߂ĺu{0}vł", m.Method("H")); //3̃o[WĂ΂
        Console.WriteLine("̖߂ĺu{0}vł", m.Method(5, 6));   //4̃o[WĂ΂
    }
}

AWKFp.195 overload01.cs

EL̃VOj`Max\bh̃I[o[[hNXMaxs쐬AmF悤
@Max(int,int) //2l̍őlԂ
@Max(int,int,int) //3l̍őlԂ
@Max(int[]) //z̗vf̍őlԂ

쐬

//AWKFp.195 overload01.cs
using System;
class Maxs {
    public int Max(int x, int y) { //2l̍őlԂ
        return (x > y) ? x : y;
    }
    public int Max(int x, int y, int z) { //3l̍őlԂ
        int max = (x > y) ? x : y; //2l̍ől𓾂Ă
        return (max > z) ? max : z; //3lڔׂ
    }
    public int Max(int[] a) {
        int max = a[0]; //ɐ擪vfőƂ
        for (int i = 1; i < a.Length; i++) { //擪̗̎vfSvfɂČJԂ
            if (max < a[i]) { //őlȂ
                max = a[i]; //őlXV
            }
        }
        return max; //őlԂ
    }
}
class overload01 {
    public static void Main() {
        Maxs m = new Maxs();
        Console.WriteLine("Max(3, 2) = {0}", m.Max(3, 2)); //1̃o[WĂ΂
        Console.WriteLine("Max(3, 4, 2) = {0}", m.Max(3, 4, 2)); //2̃o[WĂ΂
        int[] ma = {3, 4, 8, 2};
        Console.WriteLine("Max(3, 4, 8, 2) = {0}", m.Max(ma)); //3̃o[WĂ΂
    }
}

쐬EAbvo[W

//AWKFp.195 overload01.cs
using System;
class Maxs {
    public int Max(int x, int y) { //@2l̍őlԂ
        int[] w = {x, y}; //2lzɂ
        return Max(w); //BĂ
    }
    public int Max(int x, int y, int z) { //A3l̍őlԂ
        int[] w = {x, y, z}; //3lzɂ
        return Max(w); //BĂ
    }
    public int Max(int[] a) { //Bz̍őlԂ
        int max = a[0]; //ɐ擪vfőƂ
        for (int i = 1; i < a.Length; i++) { //擪̗̎vfSvfɂČJԂ
            if (max < a[i]) { //őlȂ
                max = a[i]; //őlXV
            }
        }
        return max; //őlԂ
    }
}
class overload01 {
    public static void Main() {
        Maxs m = new Maxs();
        Console.WriteLine("Max(3, 2) = {0}", m.Max(3, 2)); //1̃o[WĂ΂
        Console.WriteLine("Max(3, 4, 2) = {0}", m.Max(3, 4, 2)); //2̃o[WĂ΂
        int[] ma = {3, 4, 8, 2};
        Console.WriteLine("Max(3, 4, 8, 2) = {0}", m.Max(ma)); //3̃o[WĂ΂
    }
}

oFAWKFp.195 overload01.cs

QlFAbvo[W̗Lp

EL̃VOj`Max\bh̃I[o[[hNXMaxs쐬ł
@Max(int,int) //2l̍őlԂ
@Max(int,int,int) //3l̍őlԂ
@Max(double,duuble,duuble,duuble) //4l̍őlԂ
@Max(double[]) //z̗vf̍őlԂ

//QlFAbvo[W̗Lp
using System;
class Maxs {
    public int Max(int x, int y) { //@2l̍őlԂ
        double[] w = {x, y}; //2lzɂ
        return (int)Max(w); //CĂсA߂lintɃLXg
    }
    public int Max(int x, int y, int z) { //A3l̍őlԂ
        double[] w = {x, y, z}; //3lzɂ
        return (int)Max(w); //CĂсA߂lintɃLXg
    }
    public double Max(double x, double y, double z, double a) { //B4l̍őlԂ
        double[] w = {x, y, z, a}; //4lzɂ
        return Max(w); //CĂ
    }
    public double Max(double[] a) { //Cz̍őlԂ
        double max = a[0]; //ɐ擪vfőƂ
        for (int i = 1; i < a.Length; i++) { //擪̗̎vfSvfɂČJԂ
            if (max < a[i]) { //őlȂ
                max = a[i]; //őlXV
            }
        }
        return max; //őlԂ
    }
}
class overload01 {
    public static void Main() {
        Maxs m = new Maxs();
        Console.WriteLine("Max(3, 2) = {0}", m.Max(3, 2)); //1̃o[WĂ΂
        Console.WriteLine("Max(3, 4, 2) = {0}", m.Max(3, 4, 2)); //2̃o[WĂ΂
        Console.WriteLine("Max(3.3, 4.4, 2.2, 6.6) = {0}", m.Max(3.3, 4.4, 2.2, 6.6)); //3̃o[WĂ΂
        Console.WriteLine("Max(3.3, 4.4, 8.8) = {0}", m.Max(new double[] {3.3, 4.4, 8.8})); //4̃o[WĂ΂
    }
}

\Fp.197umain\bh̃I[o[[hvĊJ܂
